class Solution
{
public:
    int findJudge(int n, vector<vector<int>> &trust)
    {
        if (n == 1)
        {
            return 1;
        }
        unordered_map<int, int> trustedCount;
        unordered_set<int> trustOthers;
        for (auto &t : trust)
        {
            trustOthers.insert(t[0]);
            ++trustedCount[t[1]];
        }
        for (auto &[person, trusted] : trustedCount)
        {
            if (trusted == n - 1 && !trustOthers.count(person))
            {
                return person;
            }
        }
        return -1;
    }
};